package com.tyersoft.kickcalendar.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tyersoft.kickcalendar.R;
import com.tyersoft.kickcalendar.calendar.Event;

public class HardTaskAdapter extends TaskAdapter {
	
	final String CELL_IN_TIME = "+";
	final String CELL_IN_PAST = "<";

	private final int name_width;
	private final int time_width;
	private final int hour_width;
	private final int height;

	public HardTaskAdapter(Context context, ArrayList<Event> events) {
		super(context,events);

		Resources resources = context.getResources();

		name_width = resources.getDimensionPixelSize(R.dimen.table_timedevent_name_width);
		time_width = resources.getDimensionPixelSize(R.dimen.table_timedevent_time_width);
		hour_width = resources.getDimensionPixelSize(R.dimen.table_timedevent_hour_width);
		
		height = resources.getDimensionPixelSize(R.dimen.table_timedevent_height);
	}

	@Override
	public int getRowCount() {
		return events.size();
	}

	@Override
	public int getColumnCount() {
		return 1+1+23; // task name + time + timesheet
	}

	@Override
	public int getWidth(int column) {
		switch (column) {
			case -1: return name_width;
			case 0: return time_width;
			default: return hour_width;
		}
	}

	@Override
	public int getHeight(int row) {
		return height;
	}

	@Override
	public String getCellString(int row, int column) {
		
		// FIRST ROW = HEADER
		if (row == -1) {
			switch (column) {
				case -1: return getContext().getString(R.string.timedevent_table_title);
				case 0:  return getContext().getString(R.string.timedevent_table_time);
				default: return String.valueOf(column-1);
			}
		}
		
		if (row < events.size()) {
			switch (column) {
				// FIRST COLUMN = EVENT NAME
				case -1: return events.get(row).title;
				// SECOND COLUMN = EVENT TIME
				case 0: return events.get(row).TimeInterval();
				default: return events.get(row).inTime(column-1) ? CELL_IN_TIME : (events.get(row).inPast(column-1) ? CELL_IN_PAST : "");
			}
		}
		return "(" + row + "," + column + ")";
	}

	@Override
	public int getLayoutResource(int row, int column) {
		final int layoutResource;
		switch (getItemViewType(row, column)) {
			case 0:
				layoutResource = R.layout.item_table1_header;
			break;
			case 1:
				layoutResource = R.layout.item_table1_name;
			break;
			case 2:
				layoutResource = R.layout.item_table1;
			break;
			default:
				throw new RuntimeException("wtf?");
		}
		return layoutResource;
	}

	@Override
	public int getItemViewType(int row, int column) {
		if (row < 0) {
			return 0;
		} else if (column < 0) {
			return 1;
		} else {
			return 2;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public View getView(int row, int column, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = getInflater().inflate(getLayoutResource(row, column), parent, false);
		}
		TextView v = (TextView) convertView.findViewById(android.R.id.text1);
		CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
		TextView vP = (TextView) convertView.findViewById(R.id.tvTableItemNamePriority);
		
		if (getEvent(row) != null) {
			if (getEvent(row).project.length() > 0)
				setTextToView(convertView, R.id.tvProject, '#'+getEvent(row).project);
			if (getEvent(row).context.length() > 0)
				setTextToView(convertView, R.id.tvContext, '#'+getEvent(row).context);
			if (vP != null)
				vP.setBackgroundColor(convertView.getContext().getResources().getColor(Event.getPriorityColor(getEvent(row).priority)));
		}
		
		setGravity(row,column,v);
		
		setEventRef(row,column,v);
		setEventRef(row,column,cb);

		setListenerForEditEvent(row,column,v);
		setListenerForMenuEvent(row, column, v);
		setListenerForCheckEvent(row,column,cb);

		String val = getCellString(row, column);
		if ((row > -1) && (column > 0)) {
			int ResId = R.drawable.item_table1;
			if (val.equals(CELL_IN_TIME))
				ResId = R.drawable.item_table1_marked;
			if (val.equals(CELL_IN_PAST))
				ResId = R.drawable.item_table1_past;
			((LinearLayout) convertView).setBackgroundResource( ResId ); 
			v.setText(val.equals(CELL_IN_TIME) || val.equals(CELL_IN_PAST) ? "" : val );
		}
		else {
			//((LinearLayout) convertView).setBackgroundResource( val.equals("+") ? R.drawable.item_table1_marked : R.drawable.item_table1 );
			v.setText(val);
		}
		
		return convertView;
	}
}
